﻿using PRO.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PRO.Admin;
using PRO.Config;

namespace web.Manage
{
    public partial class login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (CVGRequest.GetQueryString("action") == "logout")
                {
                    string username = Utils.GetCookie(Utils.MD5("username")).DEC_Decode(BaseConfigs.DEC_KEY);
                    Utils.WriteCookie(Utils.MD5("username"), string.Empty, 0);
                    if (!Utils.StrIsNullOrEmpty(username))
                        Utils.WriteCookie(Utils.MD5(username), string.Empty, 0);
                }
            }
            this.txtUserName.Focus();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (Utils.StrIsNullOrEmpty(this.txtUserName.Text) || Utils.StrIsNullOrEmpty(this.txtPwd.Text))
            {
                this.lblmsg.Text = "请输入用户名和密码！";
                return;
            }
            else if (this.txtCode.Text.IsNullOrWhiteSpace())
            {
                this.lblmsg.Text = "请输入4位验证码！";
                return;
            }
            else
            {
                if (Session["HHC_ValidateCode"] == null)
                {
                    this.lblmsg.Text = "验证码已经失效，请重新输入！";
                    return;
                }
                else
                {
                    if (!this.txtCode.Text.ToLower().Equals(Session["HHC_ValidateCode"].ToString().ToLower()))
                    {
                        this.lblmsg.Text = "验证码输入不正确！";
                        return;
                    }
                    else
                    {
                        string msg = null;
                        if (AdminLib.Login(this.txtUserName.Text, Utils.MD5(this.txtPwd.Text), out msg))
                        {
                            Response.Redirect("/Manage/default.aspx");
                        }
                        else
                        {
                            this.lblmsg.Text = msg;
                        }
                    }
                }
            }
        }
    }
}